-- pgsql_test.lua
import("db/pgsql_mgr.lua")

local log_debug = logger.debug

local timer_mgr = quanta.get("timer_mgr")
local pgsql_mgr = quanta.get("pgsql_mgr")

timer_mgr:once(2000, function()
    log_debug("pgsql db start test!")
    local code, res_oe = pgsql_mgr:query(1, "drop table if exists test_pgsql")
    log_debug("db drop table code: {}, res = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "create table if not exists test_pgsql (id serial primary key, pid int, value int)")
    log_debug("db create table code: {}, res = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "insert into test_pgsql (pid, value) values (123457, 40)")
    log_debug("db insert code: {}, res_oe = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "insert into test_pgsql (pid, value) values (123456, 40)")
    log_debug("db insert code: {}, res_oe = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "select count(*) as count from test_pgsql where pid=123457")
    log_debug("db select code: {}, count = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "select * from test_pgsql where pid = 123456")
    log_debug("db select code: {}, res_oe = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "update test_pgsql set pid = 123454, value = 20 where pid = 123456 limit 1")
    log_debug("db update code: {}, err = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "select * from test_pgsql")
    log_debug("db select code: {}, res_oe = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "delete from test_pgsql where pid = 123457")
    log_debug("db delete code: {}, err = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "replace into test_pgsql (id, pid, value) values (1, 123457, 40)")
    log_debug("db replace code: {}, count = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "select * from test_pgsql")
    log_debug("db select code: {}, res_oe = {}", code, res_oe)
    code, res_oe = pgsql_mgr:query(1, "select count(*) as count from test_pgsql where pid=123454")
    log_debug("db count code: {}, count = {}", code, res_oe)
    --Prepared Statements
    code, res_oe = pgsql_mgr:prepare("myinsert", "insert into test_pgsql (pid, value) values ($1, $2)")
    log_debug("db prepare code: {}, count = {}", code, res_oe)
    code, res_oe = pgsql_mgr:execute(1, "myinsert", 55555, 66666)
    log_debug("db execute code: {}, count = {}", code, res_oe)
    code, res_oe = pgsql_mgr:prepare("myselect", "select * from test_pgsql where pid = $1")
    log_debug("db prepare2 code: {}, count = {}", code, res_oe)
    code, res_oe = pgsql_mgr:execute(1, "myselect", 55555)
    log_debug("db execute2 code: {}, count = {}", code, res_oe)
end)